﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using RuoTeng.Util;

namespace RuoTeng.Windows.Forms
{
    public partial class Buy : RuoTengControl
    {
        public Buy()
        {
            InitializeComponent();         
            this.CreatePictureBox(RuoTeng.Windows.Forms.Resources.Buy, "pictureBox_buy");           
        }
       
        private void button1_Click(object sender, EventArgs e)
        {
            string mc = MachineCode.GetNewMachinecode();   
            string touser = "尊敬的用户，您的机器码是[" + mc + "],\n\r如需购买请查看上面的购买方式！\n\r客服会全程服务您！机器码已经帮您复制！您只要粘贴【Ctrl+V】一下即可！";
            MessageBox.Show(touser);
            if (mc != null)
            {
                Clipboard.SetDataObject(mc);
            }         
        }

        /// <summary>
        /// 购买软件 模块的授权信息处理函数
        /// </summary>
        /// <param name="sort"></param>
        public override void Authorization(SortedList<string, object> sort)
        {
            string endTime     =  GetValue("endtime", "", sort);
            string verionType  =  GetValue("demo", "试用版", sort);
            this.SetHomeData(endTime,verionType);
            base.Authorization(sort);
        }

        private void SetHomeData(string endTime,string verionType)
        {
            try
            {
                DateTime datetime = Convert.ToDateTime(endTime);
                TimeSpan left     = new TimeSpan(datetime.Ticks);
                TimeSpan now      = new TimeSpan(System.DateTime.Now.Ticks);
        ///检测授权是否已经过期,如果没有过期计算剩余的天数
                int days          = left.Days - now.Days;
                if (days > 0)
                {
                    this.label_endtime.Text = "还剩" + days + "天【" + datetime.ToShortDateString() + "】";
                    this.label_endtime.ForeColor = Color.Green;
                }
                else
                {
                    this.label_endtime.Text = "已过期【" + datetime.ToShortDateString() + "】";
                    this.label_endtime.ForeColor = Color.Red;
                }
                this.label_softtype.Text = verionType;
            }
            catch
            {
                this.label_softtype.Text = verionType;
                this.label_endtime.Text = "已过期";
                this.label_endtime.ForeColor = Color.Red;
            }
        }
    }
}
